package tw.com.test01;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * @author ttol
 *
 */
public class Exam extends Activity {
	protected ListView list;
	protected TextView text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exam);
		
		loadViews();
	}


	Question[] questions={new Question("test1","中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文c1 vary long choice string~~~~~~~~~~~~~~~`","C2","c3","C4")
						,new Question("test2","c21","C2","c中文中文中文中文中文中文中文中文中文中文中文中文中文中文3","C4中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文")
						,new Question("test3","c31","C2","c3","C4")
						,new Question("test4","c41","C2","c3","C4")
						,new Question("test5","c51","C2","c3","C4")
						,new Question("test6","c61","C2","c3","C4")
						};
	private void loadViews(){
		list = (ListView) findViewById(R.id.listView1);
		text = (TextView) findViewById(R.id.textView1);
		QuestionAdapter adapter = new QuestionAdapter(this,questions);
        list.setAdapter(adapter);
        questions[0].Question="Good Question";
	}
	

}


class QuestionAdapter extends ArrayAdapter<Question>{
	public Question Questions[];
	private Context context;
	public QuestionAdapter(Context context, Question[] objects) {
		super(context, android.R.layout.simple_list_item_1, objects);
		// TODO Auto-generated constructor stub
		Questions=objects;
		this.context=context;
	}

	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//return super.getView(position, convertView, parent);
		View rowView=convertView;
		if (rowView == null) {
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			rowView = inflater.inflate(R.layout.exam_item, parent, false);
			//為了要把XML所描述的Layout轉變為View , 也就是我們要的rowView所以必須使用LayoutInflater來轉化 
		}
        TextView txtQuestion = (TextView) rowView.findViewById(R.id.txtQuestion);
        RadioButton rdoChoice1=(RadioButton) rowView.findViewById(R.id.rdoChoice1);
        RadioButton rdoChoice2=(RadioButton) rowView.findViewById(R.id.rdoChoice2);
        RadioButton rdoChoice3=(RadioButton) rowView.findViewById(R.id.rdoChoice3);
        RadioButton rdoChoice4=(RadioButton) rowView.findViewById(R.id.rdoChoice4);
        
        txtQuestion.setText(Questions[position].Question);
        rdoChoice1.setText(Questions[position].Choice1);
        rdoChoice2.setText(Questions[position].Choice2);
        rdoChoice3.setText(Questions[position].Choice3);
        rdoChoice4.setText(Questions[position].Choice4);
		return rowView;
	}
	
}